package day05;

import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
        int[] arr1 = {5, 7, 10,20,22,25,27,39,42,47,50};
        int n = binarySearch(arr1, 43);
        System.out.println(n);

        /*
         * 找到一组数据中最大的100个
         */
        int[] arr = {10,11, 7,16, 9,16,18,6,9,20,12,78,33,26, 4,99,22};
        int[] max = new int[5];
        for(int i=0; i<arr.length; i++){
            int num = arr[i];
            int index = binarySearch(max, num);
            if (index==-1 || index==0){
                continue;
            }
            if (index<0){
                index = -index-1;
            }
            for (int j=1; j<index; j++){
                max[j-1]=max[j];
            }
            max[index-1]=num;
        }
        System.out.println(Arrays.toString(max));
    }
    public static int binarySearch(int[] arr, int key){
        int low = 0;
        int high = arr.length-1;
        while (low <= high){
            int mid = (low + high) >> 1;
            if (arr[mid] > key){
                high = mid-1;
            }else if (arr[mid]<key){
                low = mid+1;
            }else if (arr[mid]==key){
                return mid;
            }
        }
        System.out.println(low +" "+high);
        return -low-1;
    }
}
